package org.jeecg.modules.water.service.impl;

import org.jeecg.modules.water.entity.LwkjJointWaterCon;
import org.jeecg.modules.water.entity.LwgyJointWaterDaily;
import org.jeecg.modules.water.entity.LwgyJointWaterCmd;
import org.jeecg.modules.water.mapper.LwgyJointWaterDailyMapper;
import org.jeecg.modules.water.mapper.LwgyJointWaterCmdMapper;
import org.jeecg.modules.water.mapper.LwkjJointWaterConMapper;
import org.jeecg.modules.water.service.ILwkjJointWaterConService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 监控
 * @Author: jeecg-boot
 * @Date:   2024-04-02
 * @Version: V1.0
 */
@Service
public class LwkjJointWaterConServiceImpl extends ServiceImpl<LwkjJointWaterConMapper, LwkjJointWaterCon> implements ILwkjJointWaterConService {

	@Autowired
	private LwkjJointWaterConMapper lwkjJointWaterConMapper;
	@Autowired
	private LwgyJointWaterDailyMapper lwgyJointWaterDailyMapper;
	@Autowired
	private LwgyJointWaterCmdMapper lwgyJointWaterCmdMapper;
	
	@Override
	@Transactional
	public void saveMain(LwkjJointWaterCon lwkjJointWaterCon, List<LwgyJointWaterDaily> lwgyJointWaterDailyList,List<LwgyJointWaterCmd> lwgyJointWaterCmdList) {
		lwkjJointWaterConMapper.insert(lwkjJointWaterCon);
		if(lwgyJointWaterDailyList!=null && lwgyJointWaterDailyList.size()>0) {
			for(LwgyJointWaterDaily entity:lwgyJointWaterDailyList) {
				//外键设置
				entity.setIspid(lwkjJointWaterCon.getId());
				lwgyJointWaterDailyMapper.insert(entity);
			}
		}
		if(lwgyJointWaterCmdList!=null && lwgyJointWaterCmdList.size()>0) {
			for(LwgyJointWaterCmd entity:lwgyJointWaterCmdList) {
				//外键设置
				entity.setIspid(lwkjJointWaterCon.getId());
				lwgyJointWaterCmdMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(LwkjJointWaterCon lwkjJointWaterCon,List<LwgyJointWaterDaily> lwgyJointWaterDailyList,List<LwgyJointWaterCmd> lwgyJointWaterCmdList) {
		lwkjJointWaterConMapper.updateById(lwkjJointWaterCon);
		
		//1.先删除子表数据
		lwgyJointWaterDailyMapper.deleteByMainId(lwkjJointWaterCon.getId());
		lwgyJointWaterCmdMapper.deleteByMainId(lwkjJointWaterCon.getId());
		
		//2.子表数据重新插入
		if(lwgyJointWaterDailyList!=null && lwgyJointWaterDailyList.size()>0) {
			for(LwgyJointWaterDaily entity:lwgyJointWaterDailyList) {
				//外键设置
				entity.setIspid(lwkjJointWaterCon.getId());
				lwgyJointWaterDailyMapper.insert(entity);
			}
		}
		if(lwgyJointWaterCmdList!=null && lwgyJointWaterCmdList.size()>0) {
			for(LwgyJointWaterCmd entity:lwgyJointWaterCmdList) {
				//外键设置
				entity.setIspid(lwkjJointWaterCon.getId());
				lwgyJointWaterCmdMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		lwgyJointWaterDailyMapper.deleteByMainId(id);
		lwgyJointWaterCmdMapper.deleteByMainId(id);
		lwkjJointWaterConMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			lwgyJointWaterDailyMapper.deleteByMainId(id.toString());
			lwgyJointWaterCmdMapper.deleteByMainId(id.toString());
			lwkjJointWaterConMapper.deleteById(id);
		}
	}
	
}
